using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Web;

namespace Soyuz5.Utility.Web
{
    public class Html
    {
        /// <summary>
        /// Renders a Javascript script include tag and appends the assembly's hash code as a querystring to the file 
        /// to ensure users load the latest code file when the code is updated.
        /// </summary>
        /// <param name="type">Pass a type so we can get a version from an assembly.  This should be a class in your web project.</param>
        public static string Javascript(Type typeWithVersion, params string[] urls)
        {
            StringBuilder sb = new StringBuilder();
            Version v = typeWithVersion.Assembly.GetName().Version;
            string version = v.ToString();

            foreach (string url in urls)
                sb.AppendLine(string.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}?v={1}\"></script>",
                    url, version));

            return sb.ToString();
        }

        /// <summary>
        /// Renders a CSS include tag and appends the assembly's hash code as a querystring to the file
        /// to ensure users load the latest code file when the code is updated.
        /// </summary>
        /// <param name="type">Pass a type so we can make a hash of its assembly.  This should be a class in your web project.</param>
        public static string StyleSheet(Type typeWithVersion, params string[] urls)
        {
            StringBuilder sb = new StringBuilder();
            Version v = typeWithVersion.Assembly.GetName().Version;
            string version = v.ToString();

            foreach (string url in urls)
                sb.AppendLine(string.Format("<link rel=\"Stylesheet\" type=\"text/css\" href=\"{0}?v={1}\" />",
                         url, HttpUtility.UrlEncode(version)));
            return sb.ToString();
        }
    }
}
